package com.interview.one.controller;

import com.interview.one.common.BusinessException;
import com.interview.one.common.CommonRes;
import com.interview.one.common.EmBusinessError;
import com.interview.one.domain.GetCategoryReq;
import com.interview.one.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    public CommonRes getCategory(@RequestBody GetCategoryReq req) throws BusinessException {
        List list = this.categoryService.getCategoryList(req.getParentId());
        if ((list == null) || (list.size() == 0)) {
            throw new BusinessException(EmBusinessError.NO_DATA_ERROR);
        }
        return CommonRes.create(list);
    }
}
